﻿var Comment = {
    postComment: function () {
        var data = {
            articleid: $("#articleId").val(),
            nick: $("#nick").val(),
            qq: $("#qq").val(),
            email: $("#email").val(),
            content: $("#commentArea").val()
        };
        if (Comment.validInput(data)) {
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/Comment/Edit",
                async: false,
                data: data,
                beforeSend: function () {
                    $("span#msg").html("正在提交评论，请稍后...");
                },
                complete: function (data) {
                    if (data.status == 200) {
                        $("span#msg").html("提交成功");
                        Comment.getComment($("#articleId").val());
                        $("#nick").val("");
                        $("#qq").val("");
                        $("#email").val("");
                        $("#commentArea").val("");
                        $("#checkCode").val("");
                    }
                    else
                        $("span#msg").html("提交失败" + data.status);
                },
            });
        }
    },    
    //验证评论内容是否符合格式
    validInput: function (data) {
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var qq = /^\d{5,12}$/;

        var qqText = data.qq;
        var emailText = data.email;
        var yuan = data.content.replace(/\<[^>]*>/g, "");
        //var comment = $.trim($("#commentArea").val());
        if (qqText != "") {
            if (!qq.test($("#qq").val())) {
                alert('提示\n\nQQ号码格式不正确！');
                $("#qq").focus();
                return false;
            }
        }
        if (emailText != "") {
            if (!myreg.test($("#email").val())) {
                alert('提示\n\n请输入有效的E_mail！');
                $("#email").focus();
                return false;
            }
        }
        if (yuan == "") {
            alert('提示\n\n请输入评论内容！');
            return false;
        }
        return true;
    }
}
DateFormat = {
    formatTime: function (val) {
        var re = /-?\d+/;
        var m = re.exec(val);
        var d = new Date(parseInt(m[0]));
        // 按【2012-02-13 09:09:09】的格式返回日期
        return d.format("yyyy/MM/dd hh:mm:ss");
    },
}
Date.prototype.format = function (format) //author: meizz
{
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
